<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/27
 * Time: 14:58
 */

namespace app\model;

use think\Db;
use app\model\StationOrder as ModelStationOrder;
use app\model\UserOrder as ModelUserOrder;

use app\model\UserOrderLog as ModelUserOrderLog;
class StationOrderLog extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'station_order_log';
    }

    /**
     * 类入口
     * @return StationOrderLog
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }
    /*
    * 添加站点订单记录
    */
    public function  StationOrderLogAdd($stationId,$operatorId,$orderId,$isPick,$type= 1,$remark='')
    {
        $data['station_id'] = $stationId;// ModelStationOrder::getInstance()->getStationId($operatorId);
        $data['order_id'] = $orderId;
        $data['is_pick'] = $isPick;
        $data['member_id'] = $operatorId;
        $data['state'] = (int)ModelUserOrder::getInstance()->toValue(['order_id' => $orderId],'order_state');
        $data['describe'] = (string)ModelUserOrderLog::getInstance()->getDescribe($orderId);
        $data['order_number'] = (string)ModelUserOrder::getInstance()->toValue(['order_id' => $orderId],'order_sn');
        $data['remark'] = $remark;
        $data['type'] = $type;
        return $this->toAdd($data);
    }

    /**
     * 业务信息->出入库列表分页总数
     * @param array $where
     * @param string $field
     * @param null $firstRow
     * @param null $listRows
     * @param null $order
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function toLeftJoinCount($where = [])
    {
        return Db::name($this->db_name)->where($where)->group("DATE_FORMAT(gmt_create,'%Y-%m-%d')")->count();
    }

    /**
     * 业务信息->出入库列表
     * @param array $where
     * @param string $field
     * @param null $firstRow
     * @param null $listRows
     * @param null $order
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function toLeftJoinSelect($where = [], $field = '*', $firstRow = null, $listRows = null, $order = null)
    {
        return Db::name($this->db_name)->field($field)->where($where)->limit($firstRow, $listRows)->group("substr(gmt_create,1,10)")->order($order)->select();
    }


}